package org.example.messagepack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.msgpack.MessagePack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Aaron
 * @since 2020/12/17 10:31
 * <p>描述：</p>
 */
public class MessagePackEncoder extends MessageToByteEncoder<Object> {
    /**
     * 使用 slf4j 日志框架
     */
    private final static Logger log = LoggerFactory.getLogger(MessagePackEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        // 序列化
        byte[] raw = new MessagePack().write(o);
        byteBuf.writeBytes(raw);

        log.info("raw.length = {}", raw.length);
    }

}
